var player = new Player();
var other = new Array();

// array of all the levels in our game
var levels = new Array();//[new Level()];

// the current level index into our levels array
var currentLevel = 0;



var _loadTimer;
var _actionTimer;
var miniMapVisible = false;

TypeOfLevel = {
    Dungeon: false,
    Cave: true
};
var typeOfLevel = TypeOfLevel.Dungeon;

// basic camera object
var camera = { x: 0, y: 0 };


// handle keyboard controls
var keysDown = {};
	addEventListener(
		"keydown",
		function(e) {
			for (var k in Keys) {
				if (Keys[k] == e.keyCode) {
					keysDown[e.keyCode] = true;
					if (e.preventDefault) { e.preventDefault(); }
					return true;
				}
			}
		},
		false);
	addEventListener(
		"keyup",
		function(e) {
			for (var k in Keys) {
				if (Keys[k] == e.keyCode) {
					delete keysDown[e.keyCode];
					if (e.preventDefault) { e.preventDefault(); }
					return true;
				}
			}
		},
		false);

	if (!window.requestAnimationFrame) {
    window.requestAnimationFrame = (function() {
        return window.requestAnimationFrame ||
               window.webkitRequestAnimationFrame ||
               window.mozRequestAnimationFrame ||
               window.oRequestAnimationFrame ||
               window.msRequestAnimationFrame || 
			   function(callback, element) { window.setTimeout(callback, 1000/35); };//1000 / 60// 
		})();
	}

//----------------------------
function putEnvToInitState() {
	// place the player at the up stair case
	var stairs = levels[currentLevel].dungeon.getStairs();
	player.pos.x = (stairs.up.x * tileSize) + tileSize / 2 - player.size.x / 2;
	player.pos.y = (stairs.up.y * tileSize) + tileSize / 2 - player.size.y / 2;
	
	levels[currentLevel].Init();
}

function GenerateEnv() {
	//level
	levels[0] = new Level(typeOfLevel);
		
	// player
	player = new Player();
	player.setLife(5*4);
	player.setValueAttack(1);
	player.setRadius(10);
}

function UpdateLevel(elapsed) {
	var change = levels[currentLevel].update(elapsed, keysDown);

    if (change == -1) {
        if (currentLevel > 0) {
            currentLevel--;
			player.pos.x = levels[currentLevel].playerPos.x;
			player.pos.y = levels[currentLevel].playerPos.y;
			
        }
    }
    else if (change == 1) {
        if (currentLevel == levels.length - 1) {
            levels.push(new Level(typeOfLevel));
        }
		levels[currentLevel].playerPos.x = player.pos.x;
		levels[currentLevel].playerPos.y = player.pos.y;
		
        currentLevel++;
		
		putEnvToInitState();
    }
	levels[currentLevel].updateEntity(elapsed, keysDown);
}
function DrawLevel(context) {
	levels[currentLevel].draw(context, camera);
	levels[currentLevel].drawEntity(context, camera);
}
function DrawTouch(context) {	
	
	//Mouve
	context.beginPath(); 
	context.strokeStyle = 'rgba(255, 255, 255, 0.5)'; 
	context.lineWidth = 6; 
	context.arc(moveTouchStartPos.x, moveTouchStartPos.y, 40,0,Math.PI*2,true); 
	context.stroke();
	context.beginPath(); 
	context.strokeStyle = 'rgba(255, 255, 255, 0.5)'; 
	context.lineWidth = 2; 
	context.arc(moveTouchStartPos.x, moveTouchStartPos.y, 60,0,Math.PI*2,true); 
	context.stroke();
	context.beginPath();
	context.beginPath(); 
	context.strokeStyle = 'rgba(255, 255, 255, 0.5)'; 
	context.arc(moveTouchPos.x, moveTouchPos.y, 40, 0,Math.PI*2, true); 
	context.stroke();	
	
	//Action A
	context.beginPath(); 
	context.strokeStyle = (moveTouchA>0) ? 'rgba(255, 0, 0, 0.5)' : 'rgba(255, 255, 255, 0.5)'; 
	context.lineWidth = 6; 
	context.arc(posTouchActionA.x, posTouchActionA.y, tolTouchActionA,0,Math.PI*2,true); 
	context.stroke();
	context.beginPath(); 
	context.strokeStyle = (moveTouchA>0) ? 'rgba(255, 0, 0, 0.5)' : 'rgba(255, 255, 255, 0.5)';
	context.lineWidth = 2; 
	context.arc(posTouchActionA.x, posTouchActionA.y, tolTouchActionA,0,Math.PI*2,true); 
	context.stroke();
	context.font = "12px Arial";
	context.fillStyle	 = (moveTouchA>0) ? 'rgba(255, 0, 0, 0.5)' : 'rgba(255, 255, 255, 0.5)';
	context.fillText("A", posTouchActionA.x-4, posTouchActionA.y+4);
	
	//Action B
	context.beginPath(); 
	context.strokeStyle = (moveTouchB>0) ? 'rgba(255, 0, 0, 0.5)' : 'rgba(255, 255, 255, 0.5)'; 
	context.lineWidth = 6; 
	context.arc(posTouchActionB.x, posTouchActionB.y, tolTouchActionB,0,Math.PI*2,true); 
	context.stroke();
	context.beginPath(); 
	context.strokeStyle = (moveTouchB>0) ? 'rgba(255, 0, 0, 0.5)' : 'rgba(255, 255, 255, 0.5)';  
	context.lineWidth = 2; 
	context.arc(posTouchActionB.x, posTouchActionB.y, tolTouchActionB,0,Math.PI*2,true); 
	context.stroke();
	context.font = "12px Arial";
	context.fillStyle	 = (moveTouchB>0) ? 'rgba(255, 0, 0, 0.5)' : 'rgba(255, 255, 255, 0.5)'; 
	context.fillText("B", posTouchActionB.x-4, posTouchActionB.y+4);
	
	//Menu
	context.beginPath(); 
	context.strokeStyle = (moveTouchMenu>0) ? 'rgba(255, 0, 0, 0.5)' : 'rgba(255, 255, 255, 0.5)';  
	context.lineWidth = 6; 
	context.arc(posTouchMenu.x, posTouchMenu.y, tolTouchMenu,0,Math.PI*2,true); 
	context.stroke();
	context.beginPath(); 
	context.strokeStyle = (moveTouchMenu>0) ? 'rgba(255, 0, 0, 0.5)' : 'rgba(255, 255, 255, 0.5)';  
	context.lineWidth = 2; 
	context.arc(posTouchMenu.x, posTouchMenu.y, tolTouchMenu,0,Math.PI*2,true); 
	context.stroke();
	context.font = "12px Arial";
	context.fillStyle	 = (moveTouchMenu>0) ? 'rgba(255, 0, 0, 0.5)' : 'rgba(255, 255, 255, 0.5)';   
	context.fillText("M", posTouchMenu.x-4, posTouchMenu.y+4);
		
	/*context.fillStyle	 = 'rgba(255, 255, 255, 0.5)'; 
	var posTouch = new Vector2(mouseX,mouseY); 
		
	if(posTouch.isCloseTo(posTouchMove, tolTouchMove))
		context.fillText("mouse : "+mouseX+", "+mouseY, mouseX, mouseY); */
}
//----------------------
function Update(elapsed) {
	if(Keys.Map in keysDown || (moveTouchMenu>0))
		miniMapVisible = !miniMapVisible;
		
    var cx = player.pos.x + player.size.x / 2;
    var cy = player.pos.y + player.size.y / 2;
    camera.x = Math.floor(cx - canvas.width / 2);
    camera.y = Math.floor(cy - canvas.height / 2);
	player.update(elapsed, keysDown);
	UpdateLevel(elapsed);	
}

function Draw() {
	context = canvas.getContext( '2d' );
	context.clearRect(0,0,canvas.width, canvas.height);
	
	DrawLevel(context);
	player.draw(context, camera);
	DrawTouch(context);
	if(miniMapVisible)
		levels[currentLevel].drawMiniMap(context, camera, canvas.width-100, 100, 1);
}

var prevTime = Date.now();
function MainLoop() {
	//clearInterval(timerLoad);
    var time = Date.now();
    var delta = (time - prevTime) / 1000.0;
    prevTime = time;

	Update(delta);
	Draw();	
    
    //window.requestAnimationFrame(MainLoop, document.getElementById('canvas'));
}
